package com.elite.groovy.dev;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @projectName: ngsapitask-tp2.6
 * @package: com.elite.groovy.dev
 * @className: Qn_Dev
 * @author: LiJT
 * @description: TODO
 * @date: 2024-09-05 19:09
 * @version: 1.0
 */
public class Sign_Dev {
    private static final Log log = LogFactory.getLog(Sign_Dev.class);

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {

        String inData = "{\"appId\":\"appId\",\"appSecret\":\"appSecret\",\"userCode\":\"userCode\",\"reqTimestamp\":\"reqTimestamp\"}";

        JSONObject jsonObject1 = JSON.parseObject(inData);
        String appId = (String) jsonObject1.get("appId");
        String appSecret = (String) jsonObject1.get("appSecret");
        String userCode = (String) jsonObject1.get("userCode");
        String reqTimestamp = (String) jsonObject1.get("reqTimestamp");
        Map<String, String> map = new HashMap(6);
        map.put("appId", appId);
        map.put("appSecret", appSecret);
        map.put("userCode", userCode);
        map.put("reqTimestamp", reqTimestamp);
        String outdata = encryptStr(map);
        System.out.println(outdata);

        // MzgxMDJiNjE0YmU4YmVmOTYwMWM3OGZlNjk3NTg0ZmZiODIyNThlNA==

    }


    public static String encryptStr(Map<String, String> params) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String result = getEncryptString(params);
        MessageDigest mDigest = MessageDigest.getInstance("SHA1");
        byte[] res = mDigest.digest(result.getBytes());
        String hexStr = String.valueOf(Hex.encodeHex(res));
        return encode(hexStr.getBytes("UTF-8"));
    }

    private static String getEncryptString(Map<String, String> params) {
        if (StringUtils.isNotEmpty((String) params.get("body"))) {
            JSONObject jsonObject = JSONObject.parseObject((String) params.get("body"), new Feature[]{Feature.OrderedField});
            if (jsonObject.containsKey("file")) {
                jsonObject.remove("file");
                params.put("body", JSONObject.toJSONString(jsonObject, new SerializerFeature[]{SerializerFeature.WriteMapNullValue}));
            }
        }

        Collection<String> keyset = params.keySet();
        List<String> list = new ArrayList(keyset);
        Collections.sort(list);
        String result = "";

        for (int i = 0; i < list.size(); ++i) {
            result = result + (String) list.get(i) + "=" + (params.get(list.get(i)) == null ? "" : (String) params.get(list.get(i))) + "&";
        }

        if (StringUtils.isNotEmpty(result) && result.endsWith("&")) {
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }

    public static String encode(byte[] data) {
        return (new Base64()).encodeAsString(data);
    }

}
